35. Matrix Inverse

Matrix Inverse

There is one more matrix operation that you will need in order to use the Kalman Filter equations: the inverse of a matrix.

Specifically when calculating the Kalman filter gain matrix \mathbf{K}, you will need to take the inverse of the \mathbf{S} matrix. The superscript ^{-1} represents the inverse of a matrix. Here is a reminder of the Kalman Filter gain equations where you can see the need for the inverse of S.

\mathbf{S_{k}} = \mathbf{H_{k}} \mathbf{P_{k|k-1}} \mathbf{H_{k}^T} + \mathbf{R_{k}}

\mathbf{K_{k}} = \mathbf{P_{k|k-1}} \mathbf{H_{k}^T} \mathbf{S_{k}}^{-1}

Formal Definition of Inverse of a Matrix

As mentioned, the inverse of a matrix \mathbf{A} would be denoted by \mathbf{A^{-1}}.

Formally, if matrix \mathbf{A} has an inverse, then

\mathbf{A} \times \mathbf{A^{-1}} = \mathbf{A^{-1}} \times \mathbf{A} = \mathbf{I}

where \mathbf{I} is an identity matrix.

Only square matrices, or in other words matrices with the same number of columns as rows, have inverses. You can see that this must be true based on the definition of the inverse and the identity matrix. The identity matrix is always a square matrix, so

if \mathbf{A} is m x n, then \mathbf{A^{-1}} has to be n x m to get a square identity matrix of m x m.

Multiplying \mathbf{A^{-1}} \mathbf{A} gives (n x m)(n x m) = n x m, which is not a square matrix unless n = m.

So in order for a matrix to have an inverse, the matrix must be square. At the same time, not all square matrices have inverses.

Relationship between a Scalar Inverse and a Matrix Inverse

In scalar math, the inverse of a number x is 1/x.

If you multiply a scalar by its inverse, you get 1:

x \times \frac{1}{x} = 1

In linear algebra, the inverse of a matrix is analogous to the scalar inverse:

\mathbf{A} \times \mathbf{A^{-1}} = \mathbf{I}

As you saw in the previous part of the lesson, the identity matrix has a similar role to the number 1.

Calculating the Inverse of a Matrix

For the purposes of this class, you will learn to calculate the inverse of a 1x1 matrix and a 2x2 matrix.

For matrices with more dimensions, the calculations become more complicated. Both Python and C++ have libraries that can calculate the inverse of a matrix such as the NumPy Library and the Eigen Library.

Inverse of a 1 x 1 matrix

For a 1 \times 1 matrix with a single element with value a, the inverse is simply \frac{1}{a}.

So the inverse of

\begin{bmatrix}25\end{bmatrix} is

\begin{bmatrix}\frac{1}{25}\end{bmatrix}.

Inverse of a 2 x 2 matrix

Say you have a matrix

\begin{bmatrix}a & b \\ c & d\end{bmatrix}

The inverse of this 2 x 2 matrix is

\frac{1}{ad - bc}\begin{bmatrix}d & -b \\ -c & a\end{bmatrix}

And you can see that if ad = bc, then the matrix does not have an inverse.

Another formula for 2x2 inverse matrix

Here is a more formal formula for the 2x2 inverse matrix.

\mathbf{A}^{-1} = \frac{1}{\text{det }\mathbf{A}} \left[\left(\text{tr } \mathbf{A}\right) \mathbf{I} - \mathbf{A}\right]

where

\text{det }\mathbf{A} is called the determinant of a matrix. For a 2x2 matrix, the determinant is ad - bc.

\text{tr } \mathbf{A} is called the trace of a matrix. The trace is the sum across the major diagonal, which in this case would be a + d.

If you multiply everything, you end up with the same equation already presented, namely:

\mathbf{A^{-1}} = \frac{1}{ad - bc}\begin{bmatrix}d & -b \\ -c & a\end{bmatrix}

Inverse of a 3 x 3 or larger matrix

Calculating the inverse of a larger matrix involves relatively complex matrix algebra theory. In this course, you will only need to calculate the inverse of a 2 x 2 matrix.

Coding the Inverse of a Matrix

You are going to write a function that calculates the inverse of a matrix. Remember that you will need to check the size of the matrix because a 1x1 matrix and a 2x2 matrix have different formulas.